﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PInitiatives.Outlook
{
    public class OutlookMeetingParser
    {
        //SINGLETON
        private static OutlookMeetingParser instance;

        private OutlookMeetingParser() { }

        public static OutlookMeetingParser getInstance() { 
            if(instance == null){
                instance = new OutlookMeetingParser();
            }
            return instance;
        }
        //----

        //znajdzie tylko wiadomosci, które są odpowiedziami na zaproszenie na iniciatywy
        //zwróci listę zaakceptowanych wiadomości
        public List<AcceptanceMessage> parseMessages(List<string> rawMessages)
        {
            List < AcceptanceMessage > acceptedMessages = new List<AcceptanceMessage>();

            foreach (string rawMessage in rawMessages) {
                string[] lines = rawMessage.Split('\n');
                //sprawdz czy jest to wiadomosc typu icalendar

                if (lines[0].Equals("BEGIN:VCALENDAR\r") && lines[3].Equals("METHOD:REPLY\r"))
                {
                    bool foundAttendee = false;
                    bool foundUID = false;
                    bool ifAccepted = false;
                    Guid UID = new Guid();
                    String attendeeEmail ="unknown_email";
                    //extract uid & attendee email
                    foreach (string line in lines)
                    {
                        //ATTENDEE;PARTSTAT=ACCEPTED:mailto:kontakttestowy2@gmail.com
                        if (line.StartsWith("ATTENDEE"))
                        {
                            foundAttendee = true;
                            String subline = line.Substring(18, line.Length - 18);
                            if (subline.StartsWith("ACCEPTED"))
                            {
                                ifAccepted = true;
                                String subline2 = subline.Substring(16, subline.Length - 17);
                                attendeeEmail = subline2;
                            }
                            else {
                                String subline2 = subline.Substring(16, subline.Length - 17);
                                attendeeEmail = subline2;
                                ifAccepted = false;
                            }
                             
                        }
                        else if (line.StartsWith("UID"))
                        {
                            foundUID = true;
                            String subline;
                            subline = line.Substring(4, line.Length - 5);
                            UID = new Guid(subline);
                        }
                        if (foundAttendee && foundUID)
                        {
                            acceptedMessages.Add(new AcceptanceMessage(UID, attendeeEmail, ifAccepted));
                            break;
                        }
                       
                    }
                }
            }
            return acceptedMessages;
        }
    }

    
    

}